﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FoodManager
{
    public class OrderClass
    {
        public int Id { get; set; }
        public string MaDonHang { get; set; }
        public string DiaChi { get; set; }
        public int IdKhachHang { get; set; }
        public DateTime NgayLap { get; set; }
        public int IdUser { get; set; }
        public string NhanVienQuanLy { get; set; }
        public string NhanVienGiaoHang { get; set; }
        public DateTime NgayGiao { get; set; }
        public byte TrangThai { get; set; }
        List<OrderDetail> listDetail = new List<OrderDetail>();

        public List<OrderDetail> ListDetail
        {
            get { return listDetail; }
        }
        dbTableAdapters.tblordersTableAdapter daOrder = new dbTableAdapters.tblordersTableAdapter();
        dbTableAdapters.tblorderdetailsTableAdapter daOrderDetail = new dbTableAdapters.tblorderdetailsTableAdapter();
        public void InsertOrder()
        {

            int orderid=0;
            try
            {
                orderid = daOrder.InsertQuery(this.IdKhachHang, MaDonHang, NgayLap, IdUser, DiaChi, NhanVienQuanLy, NhanVienGiaoHang, NgayGiao, TrangThai);
                foreach (OrderDetail detail in this.listDetail)
                {
                    daOrderDetail.Insert(detail.Idsanpham, detail.IdOrder, detail.SoLuong, detail.DonGia);
                }
            }
            catch (Exception ex)
            {
                daOrderDetail.DeleteQueryByIdorder(orderid);
                daOrder.DeleteQuery(orderid);
                
            }
        }
        public void AddDetail(OrderDetail o)
        {
            this.listDetail.Add(o);
        }
        public void Remove(int detailid)
        {
            for (int i = 0; i < this.listDetail.Count; i++)
            {
                if (listDetail[i].Id == detailid)
                {
                    this.listDetail.RemoveAt(i);
                    return;
                }
            }
        }
        public void Remove(OrderDetail o)
        {
            if (listDetail.Contains(o)) listDetail.Remove(o);
        }
    }
    public class OrderDetail
    {
        public int Id { get; set; }
        public int Idsanpham { get; set; }
        public int IdOrder { get; set; }
        public double SoLuong { get; set; }
        public decimal DonGia { get; set; }
        public string TenSanPham { get; set; }
        public decimal ThanhTien { get; set; }
        public string DonVi { get; set; }
    }
}
